package inet.app.vod.parser;

import inet.app.vod.config.AppConstants;
import inet.app.vod.object.CategoryModel;
import inet.app.vod.object.FunModel;
import inet.app.vod.object.MovieModel;
import inet.app.vod.object.NewModel;
import inet.app.vod.object.ResultJsonModel;
import inet.app.vod.object.User;

import java.util.ArrayList;
import java.util.List;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.inet.app.framework.util.JsonUtil;
import com.inet.app.framework.util.Log;

public class Parse {

	private static Gson gson = new Gson();
	private static JsonParser parser = new JsonParser();
	private final static String ERROR_CODE = "STATUS";

	static {
		JsonUtil.IGNORE_VALUE = 1;
	}

	public static ResultJsonModel parseAccount(String response) {
		ResultJsonModel result = new ResultJsonModel();
		try {
			JsonObject root = parser.parse(response).getAsJsonObject();
			int errorCode = root.get(ERROR_CODE).getAsInt();
			String error = "";
			if (errorCode != 0) {
				// error, don't parse user
				if (AppConstants.ERROR_CODES.indexOfKey(errorCode) < 0) {
					error = AppConstants.ERROR_CODES.get(1); // error undefined
				} else {
					error = AppConstants.ERROR_CODES.get(errorCode);
				}
				result.setError(error);
				return result;
			}
			User account = gson.fromJson(root.get("USER"), User.class);
			result.setResult(account);
		} catch (Exception e) {
			Log.e(e.toString());
			return null;
		}
		return result;
	}

	public static ResultJsonModel parseCategoryINews(String response) {
		ResultJsonModel result = new ResultJsonModel();
		try {
			JsonObject root = parser.parse(response).getAsJsonObject();
			int errorCode = root.get(ERROR_CODE).getAsInt();
			String error = "";
			if (errorCode != 0) {
				// error, don't parse user
				if (AppConstants.ERROR_CODES.indexOfKey(errorCode) < 0) {
					error = AppConstants.ERROR_CODES.get(1); // error undefined
				} else {
					error = AppConstants.ERROR_CODES.get(errorCode);
				}
				result.setError(error);
				return result;
			}
			JsonArray jArr = root.get("CATEGORIES").getAsJsonArray();
			List<CategoryModel> categories = new ArrayList<CategoryModel>();
			CategoryModel category;
			for (JsonElement jEle : jArr) {
				category = gson.fromJson(jEle, CategoryModel.class);
				categories.add(category);
			}
			result.setResult(categories);
		} catch (Exception e) {
			Log.e(e.toString());
			return null;
		}
		return result;
	}

	public static ResultJsonModel parseNewListHot(String response) {
		ResultJsonModel result = new ResultJsonModel();
		try {
			JsonObject root = parser.parse(response).getAsJsonObject();
			int errorCode = root.get(ERROR_CODE).getAsInt();
			String error = "";
			if (errorCode != 0) {
				// error, don't parse list hot
				if (AppConstants.ERROR_CODES.indexOfKey(errorCode) < 0) {
					error = AppConstants.ERROR_CODES.get(1); // error undefined
				} else {
					error = AppConstants.ERROR_CODES.get(errorCode);
				}
				result.setError(error);
				return result;
			}
			JsonArray jArr = root.get("LIST_HOT").getAsJsonArray();
			List<NewModel> news = new ArrayList<NewModel>();
			NewModel newObj;
			for (JsonElement jEle : jArr) {
				newObj = gson.fromJson(jEle, NewModel.class);
				news.add(newObj);
			}
			result.setResult(news);
		} catch (Exception e) {
			Log.e(e.toString());
			return null;
		}
		return result;
	}

	public static ResultJsonModel parseMovieListHot(String response) {
		ResultJsonModel result = new ResultJsonModel();
		try {
			JsonObject root = parser.parse(response).getAsJsonObject();
			int errorCode = root.get(ERROR_CODE).getAsInt();
			String error = "";
			if (errorCode != 0) {
				// error, don't parse list hot
				if (AppConstants.ERROR_CODES.indexOfKey(errorCode) < 0) {
					error = AppConstants.ERROR_CODES.get(1); // error undefined
				} else {
					error = AppConstants.ERROR_CODES.get(errorCode);
				}
				result.setError(error);
				return result;
			}
			JsonArray jArr = root.get("LIST_HOT").getAsJsonArray();
			List<MovieModel> news = new ArrayList<MovieModel>();
			MovieModel newObj;
			for (JsonElement jEle : jArr) {
				newObj = gson.fromJson(jEle, MovieModel.class);
				news.add(newObj);
			}
			result.setResult(news);
		} catch (Exception e) {
			Log.e(e.toString());
			return null;
		}
		return result;
	}

	public static ResultJsonModel parseMovieDetail(String response) {
		ResultJsonModel result = new ResultJsonModel();
		try {
			JsonObject root = parser.parse(response).getAsJsonObject();
			int errorCode = root.get(ERROR_CODE).getAsInt();
			String error = "";
			if (errorCode != 0) {
				// error, don't parse list hot
				if (AppConstants.ERROR_CODES.indexOfKey(errorCode) < 0) {
					error = AppConstants.ERROR_CODES.get(1); // error undefined
				} else {
					error = AppConstants.ERROR_CODES.get(errorCode);
				}
				result.setError(error);
				return result;
			}
			MovieModel movie = new Gson().fromJson(response, MovieModel.class);
			result.setResult(movie);
		} catch (Exception e) {
			Log.e(e.toString());
			return null;
		}
		return result;
	}

	public static ResultJsonModel parseFunList(String response) {
		ResultJsonModel result = new ResultJsonModel();
		try {
			JsonObject root = parser.parse(response).getAsJsonObject();
			int errorCode = root.get(ERROR_CODE).getAsInt();
			String error = "";
			if (errorCode != 0) {
				// error, don't parse list hot
				if (AppConstants.ERROR_CODES.indexOfKey(errorCode) < 0) {
					error = AppConstants.ERROR_CODES.get(1); // error undefined
				} else {
					error = AppConstants.ERROR_CODES.get(errorCode);
				}
				result.setError(error);
				return result;
			}
			JsonArray jArr = root.get("LIST_HOT").getAsJsonArray();
			List<FunModel> news = new ArrayList<FunModel>();
			FunModel newObj;
			for (JsonElement jEle : jArr) {
				newObj = gson.fromJson(jEle, FunModel.class);
				news.add(newObj);
			}
			result.setResult(news);
		} catch (Exception e) {
			Log.e(e.toString());
			return null;
		}
		return result;
	}

	public static ResultJsonModel parseNewDetail(String response) {
		ResultJsonModel result = new ResultJsonModel();
		try {
			JsonObject root = parser.parse(response).getAsJsonObject();
			int errorCode = root.get(ERROR_CODE).getAsInt();
			String error = "";
			if (errorCode != 0) {
				// error, don't parse list hot
				if (AppConstants.ERROR_CODES.indexOfKey(errorCode) < 0) {
					error = AppConstants.ERROR_CODES.get(1); // error undefined
				} else {
					error = AppConstants.ERROR_CODES.get(errorCode);
				}
				result.setError(error);
				return result;
			}
			String content = root.get("CONTENT").getAsString();
			result.setResult(content);
		} catch (Exception e) {
			Log.e(e.toString());
			return null;
		}
		return result;
	}

	public static String parseToken(String response) {
		String token = "";
		if (!TextUtils.isEmpty(response) && response.startsWith("token")) {
			String[] arr = response.split("=");
			if (arr.length == 2) {
				if (!arr.equals("-1") && !arr.equals("-2") && !arr.equals("-3")) {
					token = arr[1];
				}
			}
		}
		return token;
	}
}
